<?php

namespace App\Exports\Game;

use App\Exports\Export;

class UserExport extends Export
{

    /**
     * 映射标题行
     *
     * @return array
     */
    protected function headings(): array
    {
        $headings = trans('view.game.user.headings');

        return $headings;
    }

    /**
     * 映射数据行
     *
     * @param \Illuminate\Database\Eloquent\Model $item
     * @return array
     */
    protected function map($item): array
    {
        $currencies = $this->request->input('currency', []);
        $currencystr = json_encode($currencies);
        $currencystr = str_ireplace(['"COIN'], '"CNY', $currencystr);
        $currencies = json_decode($currencystr, true);

        $data = [];

        $balances = '';
        foreach ($item->wallet->balances as $key=>$balance) {
            if($currencies){
                if(in_array($key, $currencies)){
                    $key = str_replace("CNY", "COIN", $key);
                    $balances .= '  '.$key.':'.$balance;
                }
            }else{
                $key = str_replace("CNY", "COIN", $key);
                $balances .= '  '.$key.':'.$balance;
            }
        }

        return array_merge($data, [
            $item->account_id,
            $item->id,
            $item->org->name ?? $item->plat_code,
	        timestamp_to_datetime($item->registration_time).';',
	        timestamp_to_datetime($item->last_login_time).';',
            trim($balances),
        ]);
    }

    /**
     * 游戏
     *
     * @var array
     */
    protected $games;

    /**
     * 初始化操作
     */
    protected function init()
    {
        // 文件名称
        $this->filename = trans('view.game.user.filename');

        // 游戏
        $this->games = trans('view.game.games');
    }
}
